<?php
/**
 * sys variable
 * @package kernel_lib_sys
 */
load_lib('/sys/util/string');
/**
 * sys variable
 * @author pandajingjing
 * @package kernel_lib_sys
 */
class sys_variable{

	/**
	 * 系统变量实例
	 * @var object
	 */
	private static $_objInstance=NULL;

	/**
	 * 系统URL变量
	 * @var array
	 */
	private $_arrURLParam=array();

	/**
	 * Cookie变量
	 * @var array
	 */
	private $_arrCookie=array();

	/**
	 * 发送的Cookie变量
	 * @var array
	 */
	private $_arrSendCookie=array();

	/**
	 * Server变量
	 * @var array
	 */
	private $_arrServer=array();

	/**
	 * 配置变量
	 * @var array
	 */
	private $_arrConfig=array();

	/**
	 * 构造函数,解析Cookie
	 */
	private function __construct(){
		$this->_arrCookie=$this->_getCookie();
		if(isset($_SERVER['SCRIPT_FILENAME']) and false!==strpos($_SERVER['SCRIPT_FILENAME'],'launcher')){
			$this->_arrServer=$this->_getCmdParam();
		}else{
			$this->_arrServer=$this->_getServerParam();
		}
	}

	/**
	 * 析构函数
	 */
	function __destruct(){

	}

	/**
	 * 构造函数
	 */
	private function __clone(){

	}

	/**
	 * 获取实例
	 * @return object
	 */
	static function getInstance(){
		if(!(self::$_objInstance instanceof self)){
			self::$_objInstance=new self();
		}
		return self::$_objInstance;
	}

	/**
	 * 设置URL变量
	 * @param array $p_arrParam
	 */
	function setURLParam($p_arrParam){
		$this->_arrURLParam=$p_arrParam;
	}

	/**
	 * 添加配置变量
	 * @param string $p_strClass
	 * @param string $p_strKey
	 * @param mix $p_mixValue
	 */
	function addConfigParam($p_strClass,$p_strKey,$p_mixValue){
		$this->_arrConfig[$p_strClass][$p_strKey]=$p_mixValue;
	}

	/**
	 * 获取各种变量
	 * @param string $p_strKey
	 * @param string $p_strMethod
	 * @return mix
	 */
	function getParam($p_strKey,$p_strMethod='all'){
		$mixParam=NULL;
		$p_strType=strtolower($p_strMethod);
		switch($p_strMethod){
			case 'post':
				$mixParam=isset($_POST[$p_strKey])?$_POST[$p_strKey]:NULL;
				break;
			case 'get':
				$mixParam=isset($_GET[$p_strKey])?$_GET[$p_strKey]:NULL;
				break;
			case 'url':
				$mixParam=isset($this->_arrURLParam[$p_strKey])?$this->_arrURLParam[$p_strKey]:NULL;
				break;
			case 'cookie':
				$mixParam=isset($this->_arrCookie[$p_strKey])?$this->_arrCookie[$p_strKey]:NULL;
				break;
			case 'server':
				return isset($this->_arrServer[$p_strKey])?$this->_arrServer[$p_strKey]:NULL;
				break;
			case 'file':
				return isset($_FILES[$p_strKey])?$_FILES[$p_strKey]:NULL;
				break;
			default:
				$mixParam=isset($_POST[$p_strKey])?$_POST[$p_strKey]:(isset($_GET[$p_strKey])?$_GET[$p_strKey]:(isset($this->_arrURLParam[$p_strKey])?$this->_arrURLParam[$p_strKey]:(isset($this->_arrCookie[$p_strKey])?$this->_arrCookie[$p_strKey]:NULL)));
				break;
		}
		return sys_util_string::trimString(sys_util_string::delSlash($mixParam));
	}

	/**
	 * 得到访问时间
	 * @return int
	 */
	function getTime(){
		return $this->getParam('REQUEST_TIME','server');
	}

	/**
	 * 得到微秒级时间
	 * @return float
	 */
	function getMicroTime(){
		return microtime(true);
	}

	/**
	 * 获取各种变量
	 * @param string $p_strType
	 * @return mix
	 */
	function getParams($p_strType='all'){
		$mixParam=NULL;
		$p_strType=strtolower($p_strType);
		switch($p_strType){
			case 'post':
				$mixParam=$_POST;
				break;
			case 'get':
				$mixParam=$_GET;
				break;
			case 'url':
				$mixParam=$this->_arrURLParam;
				break;
			case 'cookie':
				$mixParam=$this->_arrCookie;
				break;
			case 'server':
				return $this->_arrServer;
				break;
			case 'file':
				return $_FILES;
				break;
			case 'config':
				return $this->_arrConfig;
				break;
			default:
				$mixParam=array_merge($this->_arrCookie,$this->_arrURLParam,$_GET,$_POST);
				break;
		}
		return sys_util_string::trimString(sys_util_string::delSlash($mixParam));
	}

	/**
	 * 写Cookie
	 * @param string $p_strName
	 * @param string $p_strValue
	 * @param int $p_intExpire
	 * @param string $p_strPath
	 * @return true/false
	 */
	function setCookie($p_strName,$p_strValue,$p_intExpire,$p_strPath='/'){
		$this->_arrSendCookie[]=array( 
				$p_strName,
				$p_strValue,
				$p_intExpire,
				$p_strPath 
		);
	}

	/**
	 * 发送Cookie
	 */
	function sendCookie(){
		$strCookieBaseDomain=get_config('cookie_basedomain');
		foreach($this->_arrSendCookie as $arrCookie){
			setcookie($arrCookie[0],$arrCookie[1],$this->_arrServer['REQUEST_TIME']+$arrCookie[2],$arrCookie[3],$strCookieBaseDomain);
		}
	}

	/**
	 * 获取Cookie
	 * @return array
	 */
	private function _getCookie(){
		return $_COOKIE;
	}

	/**
	 * 获取服务器变量
	 * @return array
	 */
	private function _getServerParam(){
		$arrServer=array();
		$strIP=NULL;
		
		if(isset($_SERVER['HTTP_CLIENT_IP'])){
			$strIP=$_SERVER['HTTP_CLIENT_IP'];
		}
		if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
			$arrIPs=explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
			if(NULL!=$strIP){
				array_unshift($arrIPs,$strIP);
				$strIP=NULL;
			}
		}
		if(NULL==$strIP){
			$strIP=$_SERVER['REMOTE_ADDR'];
		}
		$arrServer['CLIENTIP']=$strIP;
		$arrServer['REQUEST_TIME']=$_SERVER['REQUEST_TIME'];
		$arrServer['REQUEST_TIME_STR']=date('Y-m-d H:i:s',$arrServer['REQUEST_TIME']);
		$arrServer['DOCUMENT_ROOT']=$_SERVER['DOCUMENT_ROOT'];
		$arrServer['HTTP_USER_AGENT']=$_SERVER['HTTP_USER_AGENT'];
		$arrServer['SERVER_SOFTWARE']=$_SERVER['SERVER_SOFTWARE'];
		$arrServer['REQUEST_METHOD']=$_SERVER['REQUEST_METHOD'];
		//$arrServer['SCRIPT_URI']=$_SERVER['SCRIPT_URI'];
		$arrServer['HTTP_HOST']=$_SERVER['HTTP_HOST'];
		if(''==$_SERVER['QUERY_STRING']){
			$arrServer['DISPATCH_PARAM']=$_SERVER['REQUEST_URI'];
		}else{
			$arrServer['DISPATCH_PARAM']=substr($_SERVER['REQUEST_URI'],0,0-strlen($_SERVER['QUERY_STRING'])-1);
		}
		$arrServer['REQUEST_TYPE']='Browser';
		$arrServer['SERVER_NAME']=$_SERVER['SERVER_NAME'];
		$arrServer['SERVER_ADDR']=$_SERVER['SERVER_ADDR'];
		$arrServer['HTTP_REFERER']=isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:NULL;
		return $arrServer;
	}

	/**
	 * 获取客户端变量
	 * @return array
	 */
	private function _getCmdParam(){
		$arrCmd=array();
		$arrCmd['DISPATCH_PARAM']=isset($_SERVER['argv'][1])?$_SERVER['argv'][1]:'';
		$arrCmd['REQUEST_TYPE']='Console';
		$arrCmd['REQUEST_TIME']=$_SERVER['REQUEST_TIME'];
		if($_SERVER['argc']>2){
			$arrCmd['REQUEST_ARGV']=array();
			for($i=2;$i<$_SERVER['argc'];++$i){
				if('-'==substr($_SERVER['argv'][$i],0,1)){
					$arrCmd['REQUEST_ARGV'][strtoupper(substr($_SERVER['argv'][$i],1))]=$_SERVER['argv'][++$i];
				}
			}
		}
		return $arrCmd;
	}
}